package cn.com.common.tools;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

public class HttpUtil {
	/**
	 * http get
	 * @param strUrl   get url
	 * @return server return string
	 */
	public String ServerForResultGet(String strUrl) {
		String strResult = "";
		if (strUrl == null || "".equals(strUrl.trim())) {
			return strResult;
		}
		HttpGet httpRequest = new HttpGet(strUrl.trim());
		httpRequest.setHeader("Content-Type", "application/json");
	
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse httpResponse = httpClient.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				strResult = EntityUtils.toString(httpResponse.getEntity());
			}
		} catch (ClientProtocolException e) {
			//e.printStackTrace();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return strResult;
	}

	/**
	 * http post
	 * 
	 * @param uriAPI
	 * @param poString
	 *            params
	 * @return server return
	 */
	public String ServerForResultPost(String uriAPI, String poString) {

		String result = "";
		if (uriAPI == null || "".equals(uriAPI.trim())) {
			return result;
		}
		HttpPost httpRequst = new HttpPost(uriAPI.trim());// http请求
		httpRequst.setHeader("Content-Type", "application/json");
		try {
			String ponewStr = new String(poString.getBytes("UTF-8"),
					"ISO-8859-1"); //
			StringEntity sentity = new StringEntity(ponewStr);
			httpRequst.setEntity(sentity);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequst);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);//
			}
		} catch (UnsupportedEncodingException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		} catch (ClientProtocolException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		} catch (IOException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		}
		return result;
	}

	/**
	 * input obj return string
	 * 
	 * @param uriAPI
	 * @param t
	 * @return
	 */
	public <T> String ServerForResultobjPost(String uriAPI, T t) {

		// System.out.println(uriAPI);
		String poString ="";
		if (t!=null) {
			  Gson gson = new Gson();
				poString = gson.toJson(t);
			//poString = JsonUtil.ObjToJson(t);
		}		
		String result = "";
		if (uriAPI == null || "".equals(uriAPI.trim())) {
			return result;
		}
		HttpPost httpRequst = new HttpPost(uriAPI.trim());// http请求
		httpRequst.setHeader("Content-Type", "application/json");
		try {
			String ponewStr = new String(poString.getBytes("UTF-8"),
					"ISO-8859-1"); //
			StringEntity sentity = new StringEntity(ponewStr);
			httpRequst.setEntity(sentity);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequst);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);//
			}
		} catch (UnsupportedEncodingException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		} catch (ClientProtocolException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		} catch (IOException e) {
			//e.printStackTrace();
			result = e.getMessage().toString();
		}
		return result;
	}

	/**
	 * post input obj return obj
	 * 
	 * @param uriAPI
	 * @param tobj
	 * @param c
	 * @return
	 * @throws InstantiationException
	 * @throws Exception
	 */
	public <T, R> R ServerForResultobjPost(String uriAPI, T tobj,
			Class<? extends R> c) throws InstantiationException, Exception {
		R tresult = c.newInstance();

		String poString ="";
		if (tobj!= null) {
		  Gson gson = new Gson();
			poString = gson.toJson(tobj);
			//poString = JsonUtil.ObjToJson(tobj);
		}
		String result = "";		
		if (uriAPI == null || "".equals(uriAPI.trim())) {
			return tresult;
		}
		
		HttpPost httpRequst = new HttpPost(uriAPI.trim());// http请求
		httpRequst.setHeader("Content-Type", "application/json");
		try {
			String ponewStr = new String(poString.getBytes("UTF-8"),
					"ISO-8859-1"); //
			StringEntity sentity = new StringEntity(ponewStr);
			httpRequst.setEntity(sentity);
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequst);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				result = EntityUtils.toString(httpEntity);//
				  Gson gson = new Gson();
				  tresult = gson.fromJson(result,c );
				//tresult = JsonUtil.JsonToObj(result, c);
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return tresult;
	}

	public static void wirteResponseResult(HttpServletResponse response,
			String json) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(json);
		response.getWriter().close();
	}
}
